package com.zj.album.utils;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 * Created by zhaojie on 2018/2/5.
 */

public class ProcessUtils {

    private static String getProcessName() {
        try {
            File file = new File("/proc/" + android.os.Process.myPid() + "/" + "cmdline");
            BufferedReader mBufferedReader = new BufferedReader(new FileReader(file));
            String processName = mBufferedReader.readLine().trim();
            mBufferedReader.close();
            return processName;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取进程名字，可在Application初始化时判断，若非主进程则不用执行onCreate（），提升跨进程启动的速度
     */
    public static boolean isMainProcessName(Context app) {
        String processName = getProcessName();
        if (!TextUtils.isEmpty(processName) && processName.equals(app.getPackageName())) {//判断进程名，保证只有主进程运行
            return true;
        }
        return false;
    }
}
